<?php

/**
 * This is the model class for table "{{cate_post}}".
 *
 * The followings are the available columns in table '{{cate_post}}':
 * @property string $post_id
 * @property string $cate_id
 * @property string $displayorder
 */
class CatePost extends CActiveRecord {
    /**
     * Returns the static model of the specified AR class.
     * @return CatePost the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{cate_post}}';
    }

    /**
     * add default created modified time
     */

    public function behaviors() {
        return array(
                'AutoTimestampBehavior' => array(
                        'class' => 'application.components.AutoTimestampBehavior',
                //You can optionally set the field name options here
                )
        );
    }
    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
                array('post_id, cate_id', 'required'),
                array('post_id, cate_id', 'length', 'max'=>20),
                // The following rule is used by search().
                // Please remove those attributes that should not be searched.
                array('post_id, cate_id, displayorder', 'safe', 'on'=>'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * add default desc
     */

    public function scopes() {
        return array(
                'published'=>array(
                        'order'=>'created desc',
                ),
        );

    }
    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
                'post_id' => 'Post',
                'cate_id' => 'Cate',
                'displayorder' => 'Displayorder',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('post_id',$this->post_id,true);
        $criteria->compare('cate_id',$this->cate_id,true);
        $criteria->compare('displayorder',$this->displayorder,true);

        return new CActiveDataProvider(get_class($this), array(
                        'criteria'=>$criteria,
        ));
    }

    public static function string2array($cates) {
        
        return preg_split('/\s*,\s*/',trim($cates),-1,PREG_SPLIT_NO_EMPTY);
    }

    public static function array2string($cates) {
        return implode(',',$cates);
    }

    public function updateCates($oldCates, $newCates,$postid) {
        $oldCates=self::string2array($oldCates);
        $newCates=self::string2array($newCates);
        $this->addCates(array_values(array_diff($newCates,$oldCates)),$postid);
        //$this->removeCates(array_values(array_diff($oldCates,$newCates)),$postid);
    }

    public function addCates($cates,$postid) {
        $criteria=new CDbCriteria;
        foreach($cates as $cate) {
            if(!$this->exists('post_id=:pid and cate_id=:cid',array(':pid'=>$postid,':cid'=>$cate))) {
                $cat=new CatePost;
                $cat->post_id=$postid;
                $cat->cate_id=$cate;
                $cat->save();
            }            
        }
    }

    public function removeCates($cates,$postid) {
        if(empty($cates))
            return;
        $this->deleteAll('post_id='.$postid);
    }
}